package com.data.modules.online.cgform.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.data.common.util.ConvertUtils;
import com.data.modules.online.cgform.entity.OnlCgformButton;
import com.data.modules.online.cgform.entity.OnlCgformEnhanceJs;
import com.data.modules.online.cgform.entity.OnlCgformField;
import com.data.modules.online.cgform.enums.CgformConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EnhanceJsUtil {

    private static final Logger f218a = LoggerFactory.getLogger(EnhanceJsUtil.class);

    private static final String f219b = "beforeSubmit,beforeAdd,beforeEdit,afterAdd,afterEdit,beforeDelete,afterDelete,mounted,created,show,loaded";

    private static final String f220c = "\\}\\s*\r*\n*\\s*";

    private static final String f221d = ",";

    public static void main(String[] args) {
        System.out.println(createDeleteAction("aa(row){console.log(112);}\nbb(row){console.log(row);}", "bb"));
        ArrayList arrayList = new ArrayList();
        OnlCgformButton onlCgformButton = new OnlCgformButton();
        onlCgformButton.setButtonCode("bb");
        onlCgformButton.setButtonStyle("link");
        arrayList.add(onlCgformButton);
        OnlCgformButton onlCgformButton2 = new OnlCgformButton();
        onlCgformButton2.setButtonCode("aa");
        onlCgformButton2.setButtonStyle("link");
        arrayList.add(onlCgformButton2);
        System.out.println(getDeleteActionOrButton("aa(row){console.log(112);}\nbb(row){console.log(row);}", arrayList));
    }

    public static String createDeleteAction(String str, String str2) {
        String str3 = "(" + str2 + "\\s*\\(row\\)\\s*\\{)";
        String str4 = str2 + ":function(that,row){const getAction=this._getAction,postAction=this._postAction,deleteAction=this._deleteAction;";
        String b = replaceStr(str, f220c + str3, "}," + str4);
        if (b == null) {
            b = replaceStrNotNull(str, str3, str4);
        }
        return createDeleteActionTwo(b, str2, (String) null);
    }

    public static String createDeleteActionTwo(String str, String str2, String str3) {
        String str4 = "(" + ConvertUtils.getString(str3) + str2 + "\\s*\\(\\)\\s*\\{)";
        String str5 = str2 + ":function(that){const getAction=this._getAction,postAction=this._postAction,deleteAction=this._deleteAction;";
        String b = replaceStr(str, f220c + str4, "}," + str5);
        if (b == null) {
            return replaceStrNotNull(str, str4, str5);
        }
        return b;
    }

    public static String replaceStr(String str, String str2, String str3) {
        Matcher matcher = Pattern.compile(str2).matcher(str);
        if (matcher.find()) {
            return str.replace(matcher.group(0), str3);
        }
        return null;
    }

    public static String replaceStrNotNull(String str, String str2, String str3) {
        String b = replaceStr(str, str2, str3);
        return b != null ? b : str;
    }

    public static String m210a(String str, List<OnlCgformButton> list) {
        return "class OnlineEnhanceJs{constructor(getAction,postAction,deleteAction){this._getAction=getAction;this._postAction=postAction;this._deleteAction=deleteAction;}" + str + "}";
    }

    public static String joinStr(String str, String str2) {
        String str3 = "(\\s+" + str2 + "\\s*\\(\\)\\s*\\{)";
        String str4 = str2 + ":function(that,event){";
        String b = replaceStr(str, f220c + str3, "}," + str4);
        if (b == null) {
            return replaceStrNotNull(str, str3, str4);
        }
        return b;
    }

    public static String getDeleteAction(String str) {
        return "function OnlineEnhanceJs(getAction,postAction,deleteAction){return {_getAction:getAction,_postAction:postAction,_deleteAction:deleteAction," + str + "}}";
    }

    public static String getDeleteActionOrButton(String str, List<OnlCgformButton> list) {
        return "function OnlineEnhanceJs(getAction,postAction,deleteAction){return {_getAction:getAction,_postAction:postAction,_deleteAction:deleteAction," + getButton(str, list) + "}}";
    }

    public static String getButton(String str, List<OnlCgformButton> list) {
        String a;
        if (list != null) {
            for (OnlCgformButton onlCgformButton : list) {
                String buttonCode = onlCgformButton.getButtonCode();
                if ("link".equals(onlCgformButton.getButtonStyle())) {
                    str = createDeleteAction(str, buttonCode);
                } else if ("button".equals(onlCgformButton.getButtonStyle()) || CgformUtil.f276al.equals(onlCgformButton.getButtonStyle())) {
                    str = createDeleteActionTwo(str, buttonCode, (String) null);
                }
            }
        }
        String[] split = f219b.split(",");
        int length = split.length;
        int i = 0;
        while (i < length) {
            String str2 = split[i];
            if ("beforeAdd,afterAdd,mounted,created,show,loaded".indexOf(str2) >= 0) {
                a = createDeleteActionTwo(str, str2, (String) null);
            } else {
                a = createDeleteAction(str, str2);
            }
            i++;
            str = a;
        }
        return str;
    }

    public static void setOnlCgformEnhanceJsValue(OnlCgformEnhanceJs onlCgformEnhanceJs, String str, List<OnlCgformField> list) {
        if (onlCgformEnhanceJs != null && !ConvertUtils.isEmpty(onlCgformEnhanceJs.getCgJs())) {
            String str2 = " " + onlCgformEnhanceJs.getCgJs();
            if (Pattern.compile("(\\s{1}onlChange\\s*\\(\\)\\s*\\{)").matcher(str2).find()) {
                String a = createDeleteActionTwo(str2, CgformConstant.ONLINE_JS_CHANGE_FUNCTION_NAME, "\\s{1}");
                Iterator<OnlCgformField> it = list.iterator();
                while (true) {
                    str2 = a;
                    if (!it.hasNext()) {
                        break;
                    }
                    a = joinStr(str2, it.next().getDbFieldName());
                }
            }
            onlCgformEnhanceJs.setCgJs(str2);
        }
    }

    public static void m215b(OnlCgformEnhanceJs onlCgformEnhanceJs, String str, List<OnlCgformField> list) {
        if (onlCgformEnhanceJs != null && !ConvertUtils.isEmpty(onlCgformEnhanceJs.getCgJs())) {
            String cgJs = onlCgformEnhanceJs.getCgJs();
            String str2 = str + "_" + CgformConstant.ONLINE_JS_CHANGE_FUNCTION_NAME;
            if (Pattern.compile("(" + str2 + "\\s*\\(\\)\\s*\\{)").matcher(cgJs).find()) {
                String a = createDeleteActionTwo(cgJs, str2, (String) null);
                Iterator<OnlCgformField> it = list.iterator();
                while (true) {
                    cgJs = a;
                    if (!it.hasNext()) {
                        break;
                    }
                    a = joinStr(cgJs, it.next().getDbFieldName());
                }
            }
            onlCgformEnhanceJs.setCgJs(cgJs);
        }
    }
}
